#!/usr/bin/python -u 
# -*- coding:utf-8 -*-
import sys
import xlwt

args = sys.argv 
print "   Usage: txt2xls.py input.txt ID Name"
print "   Note: input.txt must be encoded in utf-8"
if len(args) !=4:
  print "   INPUT ERROR!!!"
  exit(0)
else:
  txt = args[1]
  yourid = args[2]
  yourname = args[3]
  
f=open(txt,'r')
kecheng = []
for line in f:
  line = line.strip()
  if len(line) != 0:
    kecheng.append(line)
f.close()

for line in kecheng:
  line = line.split()
  if line[0] == 'powder':
    filename = ' '.join(line[0:3]) + '-' + yourname + '.xls'
    score = line[4]
  else:
    filename = line[0] + '-' + yourname + '.xls'
    score = line[2]
  try:
    score = round(float(score),1)
  except:
    score = score
  is_xueweike = line[-1]

  workbook = xlwt.Workbook(encoding='utf-8')
   
  worksheet = workbook.add_sheet('Sheet1')

  style1 = xlwt.easyxf(strg_to_parse="font: bold on,height 240; \
    align: wrap off, vert centre, horiz center")
  style2 = xlwt.easyxf(strg_to_parse="font: bold off,height 240; \
    align: wrap off, vert centre", num_format_str="#0.0")
  worksheet.write(0, 0, '学号', style1)
  worksheet.write(0, 1, '姓名', style1)
  worksheet.write(0, 2, '成绩', style1)
  worksheet.write(0, 3, '是否学位课', style1)
  worksheet.write(1, 0, yourid, style2)
  worksheet.write(1, 1, yourname, style2)
  worksheet.write(1, 2, score, style2)
  worksheet.write(1, 3, is_xueweike, style2)
   
  workbook.save(filename)
  
print "   Done!!!"
